<%
    Users user = Trader.getUser(request.getParameter("uid"));
	wstrader.task.traffic.di.Instruction task = user.traffic.di.dIns;

	if (!user.Authenticated()) {
		response.sendRedirect("logon.jsp");
		return;
	}
	((Tasks)task.getOwner()).clearFocus();
	task.setFocus(true);	
	Trader.window_width = "80%";
	ResultSet rs;
%>

<%@include file="../header.jsp"%>
<link href="style.css" rel="stylesheet" type="text/css">

<script language="javascript">
function doFilter()
{
	var form = document.formMain;
	form.action = "traffic.di.jsp";
	form.submit();
}

function clickCardView()
{
	var form = document.formMain;
	form.action = "traffic.di.card-view.jsp";
	form.submit();	
}

function cardViewClick(row)
{
	var form = document.formMain;
	form.delivery_instruction.value = row.id;
	dblClick(row);
}
	
function new_DI()
{
	var form = document.formMain;
	
	var e = document.getElementById("contracts");
	var strUser = e.options[e.selectedIndex].value;
	
	if (strUser != "0")
	{
		form.new_rec.value="1";
		form.action = "traffic.di.card-view.jsp";
		form.submit();
	}
	else
	{
		alert("Please select a purchase contract to select a new DI");
	}

}	
</script>
<form method="POST" name="formMain" action="" onSubmit="">				
<table width="100%"  border="0" cellpadding="0" cellspacing="0" class="style2">
  <tr>
    <td><img src="images/delivery-instruction.jpg" width="300" height="30"></td>
  </tr>
  <tr>
    <td><table width="100%"  border="0" cellpadding="1" cellspacing="0" class="style2">
      <tr bgcolor="#EEEEEE" style="font-weight:bold" align="center">
            <td width="12%">Contract Ref </td>
            <td width="13%">Status</td>
            <td width="14%">Suppiler/Buyer</td>
            <td width="18%">Origin</td>
            <td width="12%">Quality</td>
            <td width="20%">Grade&nbsp;<input type="hidden" name="grade_code" id="grade_code" class="style11" style="height:16px; width:40px; text-align:center" onChange="codeChanged(this);doPost();" onKeyUp="keyUp(this);"></td>
            <td width="11%">Last Date</td>
      </tr>
      <tr>
        <td><select name="contracts" id="contracts" size="7" class="style11" style="width:100%;" onChange="setValue('contract_no',0);doPost()">
              <option value="0" selected>All</option>
              <%task.getResponse().setOption(task.getContract_List());%>
              <%task.getResponse().setSelected("contracts");%>
            </select></td>
        <td><select name="status" id="status" size="7" class="style11" style="width:100%;" onChange="setValue('Status',0);doPost()">
              <option value="3" selected>All</option>
              <option value="1">Completed</option>
              <option value="0">Open</option>
              <%task.getResponse().setSelected("status");%>
            </select></td>
        <td><select name="supplier_buyer_id" id="supplier_buyer_id" size=7 class="style11" style="width:100%;" onChange="doPost();">
               <option value="0" selected>All</option>
               <%task.getResponse().setOption(task.getCompanyList("supplier,buyer"));%>
               <%task.getResponse().setSelected("supplier_buyer_id");%>
             </select></td>
        <td><select name="origin_id" id="origin_id" size=7 class="style11" style="width:100%;" onChange="setValue('quality_id',0);setValue('grade_id',0);doPost();">
               <option value="0" selected>All</option>
               <%task.getResponse().setOption(task.getMasterList("origin_master"));%>
               <%task.getResponse().setSelected("origin_id");%>
             </select></td>
        <td><select name="quality_id" id="quality_id" size=7 class="style11" style="width:100%;" onChange="setValue('grade_id',0);doPost();">
               <option value="0" selected>All</option>
               <%task.getResponse().setOption(task.getMasterList("quality_master"));%>
               <%task.getResponse().setSelected("quality_id");%>
             </select></td>
        <td><select name="grade_id" id="grade_id" size=7 class="style11" style="width:100%;" onChange="doPost();">
               <option value="0" selected>All</option>
               <%task.getResponse().setOption(task.getMasterList("grade_master"));%>
               <%task.getResponse().setSelected("grade_id");%>
             </select></td>
        <td><select name="ls_date" id="ls_date" size=7 class="style11" style="width: 100%;" onChange="doPost()">
					<%@include file="include/ContractLsDateList.jsp"%>
            </select></td>
      </tr>
    </table></td>
  </tr>
  <tr id="di_list_view">
    <td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><table width="100%"  border="0" cellpadding="0" cellspacing="1" class="style2">
          <script language="javascript">
	var table = 'traffic.di';
        </script>
          <tr align="center" bgcolor="#EEEEEE" style="font-weight:bold; cursor:pointer" title='Click on columns to sort' onMouseOver="window.status='Click on columns to sort'">
            <td width="22" rowspan="2" onClick="doPost();"><img src="images/refresh.gif" width="18" height="18" class="style3"></td>
            <td width="96" rowspan="2" onClick="doSort(table,'ref_number')" id="h_detail_1">DI Ref</td>
            <td width="71"  rowspan="2" onClick="doSort(table,'contract_ref_number')" style="display:none">Contract Ref </td>
            <td width="110"  rowspan="2" onClick="doSort(table,'supplier')">Supplier</td>
            <td width="68"  rowspan="2" onClick="doSort(table,'origin')">Origin</td>
            <td width="73"  rowspan="2" onClick="doSort(table,'quality')">Quality</td>
            <td width="30"  rowspan="2" onClick="doSort(table,'grade_code')">Code</td>
            <td             rowspan="2" onClick="doSort(table,'grade')">Grade</td>
            <td width="70" rowspan="2">Packing</td>
            <td colspan="3">Tons</td>
            <td colspan="3">Del. Period</td>
            <td width="15">&nbsp;</td>
          </tr>
          <tr align="center" bgcolor="#EEEEEE" style="font-weight:bold; cursor:pointer">
            <td width="60">Total</td>
            <td width="60">Del.</td>
            <td width="60">Pend.</td>
            <td width="70" onClick="doSort(table,'di.delivery_date')">Date</td>
            <td width="60" onClick="doSort(table,'di.from_time')">From</td>
            <td width="60" onClick="doSort(table,'di.to_time')">To</td>
            <td>&nbsp;</td>
          </tr>
        </table></td>
      </tr>
<tr>
  	<td><div style="overflow:scroll; height:250px;">		  
			  <table width="100%"  border="0" cellpadding="0" cellspacing="1" class="style2">
<script language="javascript">
	var table = 'traffic.di';
</script>			  
<%
	Record rc = new Record(task.getDI_ListView());
	//Record rc = new Record();
	rc.beforeFirst();
	float total_tons = 0;
	float total_delivered_tons = 0;	
	while (rc.next()) {
		//rc.setRs(rs);
		String color = "#FF0000";//task.getColor(rc.getIntValue("status"));
		float pending_tons = Math.max(rc.getFloatValue("tons") - (rc.getFloatValue("recdton")),0);
		if (rc.getIntValue("status")==1) pending_tons=0;
		float delivered_tons = rc.getFloatValue("recdton");
		total_tons += rc.getFloatValue("tons");
		total_delivered_tons += (rc.getFloatValue("recdton"));
		if (rc.getIntValue("status")==0) color = "#33CCFF";
		if (rc.getIntValue("status")==1) color = "#000011";

%>				
                <tr id=<%=rc.getIntValue("id")%> style="font-size:11px" onDblClick="cardViewClick(this);" onClick="highlightOn(this);">
                  <td width="22" align="right"><%=rc.getRow()%></td>
                  <td width="96" style="color:<%=color%>"><%=rc.getStringValue("ref_number")%></td>
                  <td width="71" style="display:none"><%//=rc.getStringValue("contract_ref_number")%></td>
                  <td width="110"><%=task.getFieldValueForId("company_master","name",rc.getIntValue("supplier_id"))%></td>
                  <td width="68"><%=task.getFieldValueForId("origin_master","name",rc.getIntValue("origin_id"))%></td>
                  <td width="73"><%=task.getFieldValueForId("quality_master","name",rc.getIntValue("quality_id"))%></td>
                  <td width="30"><%=rc.getString("grade_code")%></td>
                  <td><%=rc.getString("name")%></td>
                  <td width="70"><%=task.getFieldValueForId("packing_master","name",rc.getIntValue("packing_id"))%></td>
                  <td width="60" align="right"><%=rc.getFloat("tons")%></td>
                  <td width="60" align="right"><%=rc.getFloatValue("recdton")%></td>
                  <td width="60" align="right"><%=Utils.numberToStr(pending_tons,3)%></td>
                  <td width="70" align="center"><%=rc.getDate("delivery_date")%></td>
                  <td width="60" align="center"><%=rc.getTime("from_time")%></td>
                  <td width="60" align="center"><%=rc.getTime("to_time")%></td>
                </tr>
<%
	}
	rc.close();
%>				
          </table>
</div></td>
  </tr>	  
      <tr>
        <td><table width="100%"  border="0" cellpadding="0" cellspacing="1" class="style11">
          <tr bgcolor="#EEEEEE" style="font-weight:bold;"  align="right">
            <td width="993" align="center">Total</td>
            <td width="66"><%=Utils.numberToStr(total_tons)%></td>
            <td width="61"><%=Utils.numberToStr(total_delivered_tons)%></td>
            <td width="60"><%=Utils.numberToStr(total_tons-total_delivered_tons)%></td>
            <td width="202">&nbsp;</td>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
  <tr id="di_report" style="display:none">
    <td><table border="1" cellpadding="0" cellspacing="0" hspace="0" vspace="0" width="100%">
      <tr valign="top">
        <td height="250px">
          <table width="100%"  border="0" class="style2">
            <tr class="style1">
              <td height="42">&nbsp;</td>
              <td height="42" colspan="3">Submit more information for creating report </td>
              <td width="9%">&nbsp;</td>
            </tr>
            <tr>
              <td width="6%">&nbsp;</td>
              <td width="9%">From date </td>
              <td width="8%"><input type="text" name="report_date_from" id="report_date_from" style="width:80px"></td>
              <td width="62%"><img src="../shared/images/calendar.gif" width="20" height="20" onclick="popUpCalendar(this,'report_date_from','dd-mmm-yy');"></td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>To Date </td>
              <td><input type="text" name="report_date_to" id="report_date_to" style="width:80px"></td>
              <td><img src="../shared/images/calendar.gif" width="20" height="20" onclick="popUpCalendar(this,'report_date_to','dd-mmm-yy');"></td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td><img src="images/update.gif" width="15" height="15" onClick="doTask(4);">&nbsp;<img src="images/delete.gif" width="15" height="15" onClick="cancelReport('di');"></td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
  <tr>
  	<td><table width="100%"  border="0" cellpadding="0" cellspacing="0" class="style2">
      <tr id="di_buttons" style="display:">
        <td width="12%"><img src="images/new.gif" border="0" width="15" height="15" onClick="new_DI()"></td>
        <td width="88%" colspan="6" align="right"><img src="images/report.jpg" width="55" height="18" onClick="doReport('di');">&nbsp;<img id="bt_card_view" src="images/cardview.jpg" width="55" height="18" onClick="clickCardView();"></td>
      </tr>
    </table></td>
  </tr>
</table>
	<input type="hidden" name="uid"  id="uid"  value="<%=user.getuid()%>">
	<input type="hidden" name="task_id"  id="task_id"  value="0">
	<input type="hidden" name="sorted_field"  id="sorted_field"  value="">
	<input type="hidden" name="sorted_table"  id="sorted_table"  value="">
	<input type="hidden" name="view" id="view" value="<%=task.getRequest().getIntValue("view")%>">	
	<input type="hidden" name="delivery_instruction"  id="delivery_instruction"  value="0">
    <input name="new_rec" type="hidden" id="new_rec" value="0">
</form>
<script language="javascript">
	setFocus('grade_code');
</script>		
<%@include file="../footer.jsp"%>